package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "分类品牌管理")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Operation(summary = "分页及带条件的查询")
    @GetMapping("/list")
    public TableDataInfo getPageList(CategoryBrand categoryBrand) {
        //开启分页
        startPage();
        //调用CategoryBrandService中带条件查询的方法
        List<CategoryBrand> categoryBrandList = categoryBrandService.getPageList(categoryBrand);
        return getDataTable(categoryBrandList);
    }

    @Operation(summary = "添加分类品牌")
    @PostMapping
    public AjaxResult saveCategoryBrand(@RequestBody CategoryBrand categoryBrand) {
        //设置添加者
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        //调用自定义的保存的方法
        boolean save = categoryBrandService.saveCategoryBrand(categoryBrand);
        return toAjax(save);
    }

    @Operation(summary = "根据id删除和批量删除分类品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteCategoryBrand(@PathVariable List<Long> ids) {
        //调用自带的批量删除的方法
        boolean b = categoryBrandService.removeBatchByIds(ids);
        return toAjax(b);
    }

    @Operation(summary = "根据id查询分类品牌")
    @GetMapping("/{id}")
    public AjaxResult getCategoryBrandById(@PathVariable Long id) {
        //调用自定义的根据id查询分类品牌的方法
        CategoryBrand categoryBrand = categoryBrandService.getCategoryBrandById(id);
        return AjaxResult.success(categoryBrand);
    }

    @Operation(summary = "根据id更新分类品牌")
    @PutMapping
    public AjaxResult updateCategoryBranById(@RequestBody CategoryBrand categoryBrand){
        //设置更新者
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        //调用自定义的更新分类品牌的方法
        int flag = categoryBrandService.updateCategoryBrandById(categoryBrand);
        return toAjax(flag);
    }

    @Operation(summary = "根据分类id查询分类下所有的品牌")
    @GetMapping("/brandList/{categoryId}")
    public AjaxResult getBrandListByCategoryId(@PathVariable Long categoryId){
        //调用自定义的根据分类id查询分类下所有品牌的方法
        List<Brand> brandList = categoryBrandService.getBrandListByCategoryId(categoryId);
        return AjaxResult.success(brandList);
    }
}
